package com.dji.activationDemo;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Cache;
import com.android.volley.Network;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.BasicNetwork;
import com.android.volley.toolbox.DiskBasedCache;
import com.android.volley.toolbox.HurlStack;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import dji.log.third.Logger;

public class LoginActivity extends Activity implements View.OnClickListener{


    private EditText userNameEdit;

    private EditText passWordEdit;

    private Button loginBtn;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initUI();
    }



    private void initUI(){
        userNameEdit = findViewById(R.id.UserNameEdit);
        passWordEdit = findViewById(R.id.PassWordEdit);
        loginBtn = findViewById(R.id.LoginButton);

        loginBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.LoginButton){
            Login();
        }
    }

    private void Login(){

        String userName = userNameEdit.getText().toString();
        String password = passWordEdit.getText().toString();

        String postUrl = Constant.ctx + "equipment/login";


        Map<String,String> map=new HashMap<String,String>();
        map.put("user", userName);
        map.put("passwd", password);
        JSONObject params=new JSONObject(map);
        RequestQueue requestQueue = Volley.newRequestQueue(this);

        // 2 创建一个请求
        //JsonArrayRequest jsonObjectRequest2=......
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
                postUrl, params, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
//                Logger.e("jsonObject-->" + jsonObject);
               try {
                   int code = jsonObject.getInt("code");
                   if (code == 0){
                       LoginSuccess(jsonObject);
                   }else {
                       String msg = jsonObject.getString("msg");
                       showToast(msg);
                   }
               } catch (JSONException e) {
                   e.printStackTrace();
               }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
//                Logger.e("response-->" + volleyError.getMessage());
            }
        });

        // 3 将创建的请求添加到请求队列中
        requestQueue.add(jsonObjectRequest);
    }

    private void LoginSuccess(JSONObject jsonObject){
        try {
            JSONObject data = jsonObject.getJSONObject("data");
            Constant.EQUIPMENT_ID = data.getString("id");

            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private void showToast(final String toastMsg) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getApplicationContext(), toastMsg, Toast.LENGTH_LONG).show();
            }
        });
    }

}